package com.study.debby.smarttable

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import com.bin.david.form.core.SmartTable
import com.bin.david.form.data.column.Column
import com.bin.david.form.data.table.TableData
import com.study.debby.smarttable.bean.UserInfo
import com.study.debby.smarttable.dagger.DaggerActivityComponent
import com.study.debby.smarttable.dagger.Format
import com.study.debby.smarttable.util.*
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import javax.inject.Inject


class MainActivity : BaseActivity() {
    private var list = ArrayList<UserInfo>()
    private lateinit var smartTable: SmartTable<UserInfo>
    @Inject
    lateinit var format: Format

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        DaggerActivityComponent.create().inject(this)
        initTitleBar(true)
        setContentView(R.layout.activity_main)
        initView()
        initData(savedInstanceState)
    }

    private fun initView() {
        smartTable = findViewById(R.id.movie_table)
        initConfig(smartTable.config)
        bt_add.setOnClickListener {
            smartTable.addData(addData(), true)
        }

        iv_left.setOnClickListener {
            startActivity(Intent().setClass(this, MovieSelectActivity::class.java))
        }
    }

    //初始化数据
    private fun initData(savedInstanceState: Bundle?) {
        if (savedInstanceState != null) {
            val mList = savedInstanceState.getSerializable("list") as ArrayList<*>
            list.clear()
            for (a in mList) {
                if (a is UserInfo) {
                    list.add(a)
                }
            }
        }
        if (list.size > 0) {
            add()
        } else {
            list.addAll(addData())
            list.addAll(addData())
            add()
        }
    }

    //增加数据
    private fun add() {
        ///
        val id = Column<Long>("用户id", "id")
        id.isFixed = true
        id.isAutoMerge = true
        val name = Column<String>("用户名", "name")
        val age = Column<Int>("年龄", "age")
        val user = Column<Any>("用户信息", name, age)
        id.setOnColumnItemClickListener { _, value, _, position ->
            Toast.makeText(this, "位置$position=$value,当前日期${format.getToday()}", Toast.LENGTH_SHORT).show()
        }
        name.setOnColumnItemClickListener { _, value, _, position ->
            Toast.makeText(this, "位置$position=$value", Toast.LENGTH_SHORT).show()
        }
        age.setOnColumnItemClickListener { _, value, _, position ->
            Toast.makeText(this, "位置$position=$value", Toast.LENGTH_SHORT).show()
        }
        ///
        val course1 = Column<Int>("course1", "score.course1")
        val course2 = Column<Int>("course2", "score.course2")
        val course3 = Column<Int>("course3", "score.course3")
        val course4 = Column<Int>("course4", "score.course4")
        val score = Column<Any>("课程", course1, course2, course3, course4)
        ///
        val hobby1 = Column<String>("爱好1", "hobby.hobby1")
        val hobby2 = Column<String>("爱好1", "hobby.hobby2")
        val hobby3 = Column<String>("爱好1", "hobby.hobby3")
        val sub1 = Column<String>("二级爱好1", "hobby.sub.sub1")
        val sub2 = Column<String>("二级爱好2", "hobby.sub.sub2")
        val sub = Column<Any>("二级爱好", sub1, sub2)
        val hobby = Column<Any>("爱好", hobby1, hobby2, hobby3, sub)
        //创建时间
        val time = Column<String>("创建时间", "time")
        smartTable.tableData = TableData("用户信息", list, id, user, score, hobby, time)
    }

    //保存数据
    override fun onSaveInstanceState(savedInstanceState: Bundle?) {
        savedInstanceState!!.putSerializable("list", list)
        super.onSaveInstanceState(savedInstanceState)
    }

    override fun onBackPressed() {
        moveTaskToBack(true)
    }

    override fun onDestroy() {
        initTitleBar(false)
        super.onDestroy()
    }


}
